export default {
  state: {
    openedTab: [],
    activeTab: ''
  },
  mutations: {
    clearOpenedTab(state) {
      state.openedTab = []
    },
    clearActiveTab(state) {
      state.activeTab = ''
    },
    addTab(state, {name, path}) {
      let flag = false
      state.openedTab.forEach(item => {
        if (item.name === name && item.path === path) {
          flag = true
        }
      })
      if (!flag) {
        state.openedTab.push({
          name: name,
          path: path
        })
        state.activeTab = path
        sessionStorage.setItem('openedTab', JSON.stringify(state.openedTab))
        sessionStorage.setItem('activeTab', path)
      }
    },
    changeTab(state, path) {
      state.activeTab = path
      sessionStorage.setItem('activeTab', state.activeTab)
    },
    deductTab(state, path) {
      let tabs = state.openedTab
      state.openedTab = tabs.filter(tab => tab.path !== path);
      sessionStorage.setItem('openedTab', JSON.stringify(state.openedTab))
    },
    setOpenedTab(state, {openedTab, activeTab}) {
      state.openedTab = openedTab
      state.activeTab = activeTab
    }
  }
}
